package com.dezheyun.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

/**
 * 配置第三方授权服务
 */
@Configuration
@EnableAuthorizationServer
public class MyAuthorizationServer extends AuthorizationServerConfigurerAdapter {

    /**
     * 客户端名称
     */
    private static final String UNQ_CLIENT_NAME = "dz";

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private ClientDetailsService clientDetailsService;

    /**
     * 授权服务的权限配置信息
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.checkTokenAccess("permitAll()") //资源服务发送的检查Token是否合法的请求放过
                .tokenKeyAccess("permitAll()") //客户端发送的获取Token的服务方法
                .allowFormAuthenticationForClients(); //支持客户端表单提交
    }

    /**
     * 客户端的配置信息
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                //客户端编号唯一
                .withClient(UNQ_CLIENT_NAME)
                //重定义地址，携带的授权码或者token信息
                .redirectUris("http://www.baidu.com")
                //不自动授权，用户自己选择
                .autoApprove(false)
                //作用域  all所有 、 read 读、 writer 写
                .scopes("all")
                //客户端密码
                .secret(new BCryptPasswordEncoder().encode("123"))
                //支持授权码模式、密码模式、客户端模式、简化模式、刷新token
                .authorizedGrantTypes("authorization_code", "password", "client_credentials", "implicit", "refresh_token")
                //要访问的资源编号
                .resourceIds("dzRe");
    }

    /**
     * 访问服务的配置信息
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)  //关联对应的认证器
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)  //支持的请求Get、Post
                .tokenServices(this.tokenServices());  //设置token相关的服务
    }

    /**
     * 设置Token相关的信息
     */
    public AuthorizationServerTokenServices tokenServices() {
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        //存储方式
        tokenServices.setTokenStore(new InMemoryTokenStore());
        //支持刷新
        tokenServices.setSupportRefreshToken(true);
        //token对应的客户端信息
        tokenServices.setClientDetailsService(clientDetailsService);
        //默认的有效期
        tokenServices.setAccessTokenValiditySeconds(7200);
        return tokenServices;
    }
}
